<?php

namespace App\Http\Controllers\Dash;


use Andach\LaravelIPFS\IPFS;
use App\Http\Controllers\Controller;
use App\Model\Download;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class DownloadController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:download-create', ['only' => ['create', 'store']]);
        $this->middleware('permission:download-delete', ['only' => ['delete']]);
    }

    //
    public function index()
    {
        $data = Download::all();
        return view('dash.download.index', compact('data'));
    }

    public function create()
    {
        return view('dash.download.create');
    }

    public function edit($id)
    {
        $data = Download::find($id);
        return view('dash.download.edit', compact('data', 'id'));
    }


    public function store(Request $request)
    {

        if ($request->hasFile('up')) {
            $filenamewithextension = $request->file('up')->getClientOriginalName();

            //get filename without extension
            $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);

            //get file extension
            $extension = $request->file('up')->getClientOriginalExtension();
            $size = $request->file('up')->getSize();
            $mime = $request->file('up')->getMimeType();
            $fname = $request->file('up')->getClientOriginalName();


            //filename to store
            $filenametostore = $filename . '_' . time() . '.' . $extension;

            $request->file('up')->storeAs('public/downloads', $filenametostore);
            $re = new Download();
            $re->patch = 'storage/downloads/' . $filenametostore;
            $re->filename = $fname;
            $re->size = $size;
            $re->mime = $mime;
            $re->comment = $request->comment;
            $re->visible = $request->visible;
            $re->saveOrFail();
            return redirect()->route('dash.download');
        }
    }

    public function update($id, Request $request)
    {
        $re = Download::find($id);
        $re->title = $request->title;
        $re->text = $request->text;
        $re->save();
        return redirect(route('dash.download'));
    }

    public function delete($id)
    {
        $re = Download::find($id)->delete();
        return redirect(route('dash.download'))->with('success', $re);
    }

    public function upload(Request $request)
    {
        if ($request->provider == 'ipfs') {
            $ipfs = new IPFS();
            $irl = $ipfs->add($request->upload);
            $down = new Download();
            $down->visible = $request->visible;
            $down->provider = $request->provider;
            $down->patch = $irl;
            $down->saveOrFail();
        } else {
            $filenamewithextension = $request->file('upload')->getClientOriginalName();

            //get filename without extension
            $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);

            //get file extension
            $extension = $request->file('upload')->getClientOriginalExtension();

            //filename to store
            $filenametostore = $filename . '_' . time() . '.' . $extension;

            //Upload File
            $request->file('upload')->storeAs('public/downloads', $filenametostore);
            $down = new Download();
            $down->visible = $request->visible;
            $down->provider = 'local';
            $down->patch = asset('storage/uploads/' . $filenametostore);
            $down->saveOrFail();
        }

    }
}
